package com.example.demo.controller;

import com.example.demo.entity.User;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("thymeleaf")
public class thymeleafController {
    @GetMapping("/variable")
    public ModelAndView variable(){

        ModelAndView modelAndView =new ModelAndView("example/thymeleaf");
        String name="huixin";
        Integer age=8;
        modelAndView.addObject("name",name);
        modelAndView.addObject("age",age);
        return  modelAndView;
    }

    @GetMapping("/list")
    public ModelAndView list() throws Exception{
        ModelAndView list =new ModelAndView("example/list2");
        List list1 =new ArrayList();
        list1.add(new User("1","xiaoming"));
        list1.add(new User("2","xiaohong"));
        list1.add(new User("3","xiaowang"));
        list.addObject("list",list1);
        return list;
    }

    @GetMapping("/map")
    public ModelAndView  map() throws Exception{
        ModelAndView m =new ModelAndView("example/map");
        Map<Integer,User> map =new HashMap<>();
        map.put(1,new User("1","xiaoming"));
        map.put(2,new User("2","xiaohong"));
        map.put(3,new User("3","xiaowang"));
        m.addObject("map",map);
        return m;
    }
}
